package com.abss.vote.util;


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;

public class HttpUtil {

    public static String get(String url) throws Exception {
        OutputStreamWriter opsw = null;
        BufferedReader br = null;
        StringBuilder result = new StringBuilder();

        URL u = new URL(url);
        HttpURLConnection connection = (HttpURLConnection) u.openConnection();
        connection.setRequestMethod("GET");
        //设置是否允许数据写入(允许),因为要写随机数
        connection.setDoInput(true);
        //是否允许输出(允许)
        connection.setDoOutput(true);
        //设置连接响应时间
        connection.setConnectTimeout(5000);
        //设置参数读取时间
        connection.setReadTimeout(10000);
        //设置请求头
        connection.setRequestProperty("Content-type", "application/x-www-form-urlencoded");
        //提交请求
        opsw = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");

        opsw.flush();

        br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
        String temp = "";
        while ((temp = br.readLine()) != null) {
            result.append(temp);
        }
        return result.toString();
    }
}
